package com.rockcent.mall.lib.helper;

import com.rockcent.mall.lib.domain.CustomerFund;
import com.rockcent.mall.lib.domain.CustomerFundFlow;
import com.rockcent.mall.lib.domain.MallOrder;
import com.rockcent.mall.lib.domain.enumclass.CustomerFundFlowTransactionType;
import com.rockcent.mall.lib.domain.enumclass.FundTransactionFlow;
import com.rockcent.mall.lib.domain.enumclass.PaymentMethod;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * Created by oyhk on 2016/11/7.
 *
 */
public enum  CustomerFundFlowHelper {

    SINGLEONE;


    /**
     * 用户资金
     * @param customerFund
     * @param mallOrder
     * @return
     */
    public CustomerFundFlow create(CustomerFund customerFund, MallOrder mallOrder, BigDecimal changeAvailableAmount,BigDecimal changeFrozen,BigDecimal operationAmount, FundTransactionFlow transactionFlow, CustomerFundFlowTransactionType customerFundFlowTransactionType, String remark) {
        return this.create(customerFund,mallOrder,changeAvailableAmount,changeFrozen,operationAmount,transactionFlow,customerFundFlowTransactionType,remark,null);
    }

    /**
     * 用户资金
     * @param customerFund
     * @param mallOrder
     * @return
     */
    public CustomerFundFlow create(CustomerFund customerFund, MallOrder mallOrder, BigDecimal changeAvailableAmount, BigDecimal changeFrozen, BigDecimal operationAmount, FundTransactionFlow transactionFlow, CustomerFundFlowTransactionType customerFundFlowTransactionType, String remark, PaymentMethod paymentMethod) {
        CustomerFundFlow customerFundFlow = new CustomerFundFlow();

        if (transactionFlow == FundTransactionFlow.INCOME) {
            customerFundFlow.setAvailableAmount(customerFund.getAvailableAmount().add(changeAvailableAmount));
            customerFundFlow.setFrozen(customerFund.getFrozen().add(changeFrozen));
        }
        if (transactionFlow == FundTransactionFlow.EXPEND) {
            customerFundFlow.setAvailableAmount(customerFund.getAvailableAmount().subtract(changeAvailableAmount));
            customerFundFlow.setFrozen(customerFund.getFrozen().subtract(changeFrozen));
        }

        customerFundFlow.setTotalAmount(customerFundFlow.getAvailableAmount().add(customerFundFlow.getFrozen()));
        customerFundFlow.setCustomerFundId(customerFund.getId());
        customerFundFlow.setCustomerId(customerFund.getCustomerId());
        customerFundFlow.setOperationAmount(operationAmount);
        customerFundFlow.setMallId(mallOrder.getMallId());
        customerFundFlow.setTransactionFlow(transactionFlow);
        customerFundFlow.setTransactionType(customerFundFlowTransactionType);
        customerFundFlow.setRemark(String.format("%s-%s%s元-%s", customerFundFlowTransactionType.getValue(), transactionFlow.getValue(), operationAmount.setScale(2,BigDecimal.ROUND_HALF_UP).toString(), remark));
        customerFundFlow.setMallOrderNum(mallOrder.getOrderNum());
        customerFundFlow.setPaymentMethod(paymentMethod);
        return customerFundFlow;
    }
}
